﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class Pages_Store_Profile : System.Web.UI.Page
{
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
        var page = UserProfile.ActiveViewIndex;

        if (page == 0 && User.Identity.IsAuthenticated && !Page.IsPostBack)
        {
            GoToUserInfo.Enabled = false;
            GoToPassChange.Enabled = true;
            gotoIstoric.Enabled = true;

            txtFirst_Name.Text = Profile.FirstName;
            txtLast_Name.Text = Profile.LastName;
            txtemail.Text = Membership.GetUser(User.Identity.Name).Email;

            txtAdress.Text = Profile.Address;
            txtRegion.Text = Profile.Oras;
            txtJudet.Text = Profile.Judet;

            txtPostalCode.Text = Profile.CodPostal;

            txtMobilePhone.Text = Profile.TelefonMobil;
            txtLandline.Text = Profile.TelefonFix;

            txtCNP.Text = Profile.CNP;
            txtSeriaSiNumaru.Text = Profile.SerieSiNumar;
            ddlCetatenie.SelectedValue = Profile.Cetatenie;
            txtEliberatDe.Text = Profile.EliberatDe;
        }
        else if (page == 1)
        {
            GoToUserInfo.Enabled = true;
            GoToPassChange.Enabled = true;
            gotoIstoric.Enabled = false;
        }
        else if (page == 2) {
            GoToUserInfo.Enabled = true;
            GoToPassChange.Enabled = false;
            gotoIstoric.Enabled = true;
        }

    }
    protected void Update_Click(object sender, EventArgs e)
    {
        if (User.Identity.IsAuthenticated && UserProfile.ActiveViewIndex == 0)
        {
            Profile.FirstName = txtFirst_Name.Text;
            Profile.LastName = txtLast_Name.Text;

            Profile.Address = txtAdress.Text;
            Profile.Judet = txtJudet.Text;
            Profile.Oras = txtRegion.Text;

            Profile.CodPostal = txtPostalCode.Text;

            Profile.TelefonMobil = txtMobilePhone.Text;
            Profile.TelefonFix = txtLandline.Text;

            Profile.CNP = txtCNP.Text;
            Profile.SerieSiNumar = txtSeriaSiNumaru.Text;
            Profile.EliberatDe = txtEliberatDe.Text;

            Profile.Cetatenie = ddlCetatenie.SelectedValue;

            Profile.Save();

            var usr = Membership.GetUser(User.Identity.Name);
            usr.Email = txtemail.Text;
            Membership.UpdateUser(usr);

        }
    }
    protected void GoToPassChange_Click(object sender, EventArgs e)
    {
        GoToUserInfo.Enabled = true;
        GoToPassChange.Enabled = false;
        gotoIstoric.Enabled = true;

        UserProfile.ActiveViewIndex = 2;
    }
    protected void GoToUserInfo_Click(object sender, EventArgs e)
    {
        GoToUserInfo.Enabled = false;
        GoToPassChange.Enabled = true;
        gotoIstoric.Enabled = true;

        UserProfile.ActiveViewIndex = 0;
    }
    protected void ContinuePushButton_Click(object sender, EventArgs e)
    {
        GoToUserInfo_Click(sender, e);
    }
    protected void UserProfile_ActiveViewChanged(object sender, EventArgs e)
    {

    }
    protected void gotoIstoric_Click(object sender, EventArgs e)
    {
        GoToUserInfo.Enabled = true;
        GoToPassChange.Enabled = true;
        gotoIstoric.Enabled = false;

        UserProfile.ActiveViewIndex = 1;

        SqlConnection connection = new SqlConnection(GetConnectionString());
        SqlCommand command = new SqlCommand();

        command.Parameters.Add("username", SqlDbType.VarChar);
        command.Parameters["username"].Value = Membership.GetUser().UserName;

        command.CommandText = "SELECT Orders.ID, Orders.shipping_address, Orders.billing_address, Orders.amount, Orders.created_at, Orders.status, Orders.shiping_date FROM aspnet_Users INNER JOIN Orders ON aspnet_Users.UserId = Orders.UserID WHERE (aspnet_Users.UserName = @username)";

        command.Connection = connection;
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();
        ListView1.DataSource = reader;
        ListView1.DataBind();

        connection.Close();
    }
    
        string GetConnectionString(string connection_name = "StoreDBConnectionString")
        {
            string connection = string.Empty;

            connection = ConfigurationManager.ConnectionStrings[connection_name].ConnectionString;

            return connection;
    }
}